/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2024-12-23
 * Time: 17:26
 */
public class test2 {
    /*
    LeetCode 24 两两交换链表中的节点

     */

    public ListNode swapPairs(ListNode head) {
        if(head==null || head.next==null){
            return head;
        }
        ListNode newHead = new ListNode(0);
        ListNode tmp = newHead;
        tmp.next = head;
        while(tmp.next!=null && tmp.next.next!=null){
            ListNode node1 = tmp.next;
            ListNode node2 = tmp.next.next;
            tmp.next = node2;
            node1.next = node2.next;
            node2.next = node1;
            tmp = node1;
        }
        return newHead.next;
    }

    public class ListNode{
        int val;
        ListNode next;
        ListNode(){

        }

        ListNode(int val){
            this.val = val;
        }

        ListNode(int val,ListNode next){
            this.val = val;
            this.next = next;
        }
    }
}
